import moment from 'moment'
const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']

const timeToEng = function (time) {
  const date = new Date(time)
  return months[date.getMonth()]
}
// 日期得到年月日
const dateGetYMD = function (date) {
  if (date === "" || date === null || date === undefined){
    return ""
  }else {
    return date.split(" ")[0]
  }
}


const timeFormat = function (time, fmt, defaultStr='无') {
  if (time === undefined || time === null) {
    return defaultStr
  }
  var date = new Date()

  date.setTime(time)
  var o = {
    'M+': date.getMonth() + 1, // 月份
    'd+': date.getDate(), // 日
    'h+': date.getHours(), // 小时
    'm+': date.getMinutes(), // 分
    's+': date.getSeconds(), // 秒
    'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
    'S': date.getMilliseconds() // 毫秒
  }
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  }
  for (const k in o) {
    if (new RegExp('(' + k + ')').test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
    }
  }
  return fmt
}
const getIntervalHour=function(startDate, endDate) {
  let ms = endDate.getTime() - startDate.getTime();
  if (ms < 0) return 0;
  return ms/1000/60/60;
}
const dateFormat = function (date, fmt) {
  if (date === undefined || date === null) {
    return null
  }
  var o = {
    'M+': date.getMonth() + 1, // 月份
    'd+': date.getDate(), // 日
    'h+': date.getHours(), // 小时
    'm+': date.getMinutes(), // 分
    's+': date.getSeconds(), // 秒
    'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
    'S': date.getMilliseconds() // 毫秒
  }
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  }
  for (const k in o) {
    if (new RegExp('(' + k + ')').test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
    }
  }
  return fmt
}

// 限制输入框只能输入数字和小数
export function inputLimitNum(e, num) {
  if (!num) { //正整数
    return e.target.value.replace(/[^0-9]+/g, '');
  } else {
    let re = new RegExp("^\\d*(\\.?\\d{0," + num + "})", "g");
    return (e.target.value.match(re)[0]) || null;
  }

}


//时间戳转换成日期
const timestampTranslateToDate = (timestamp) => {
  const time = new Date(parseInt(timestamp));
  const y = time.getFullYear();
  let m = time.getMonth() + 1;
  let d = time.getDate();
  let h = time.getHours();
  let mm = time.getMinutes();
  let s = time.getSeconds();
  return y + '-' + add0(m) + '-' + add0(d) + ' ' + add0(h) + ':' + add0(mm) + ':' + add0(s);
}

//时间戳转换成日期
const timestampTranslateToDate1 = (timestamp) => {
  const time = new Date(parseInt(timestamp));
  const y = time.getFullYear();
  let m = time.getMonth() + 1;
  let d = time.getDate();
  return y + '-' + add0(m) + '-' + add0(d);
}

//转换
const add0 = (m) => {
  return m < 10 ? '0' + m : m
}


//获取年月日
const getCurrentDate = () => {
  const time = new Date();
  const y = time.getFullYear();
  let m = time.getMonth() + 1;
  let d = time.getDate();
  return y + '-' + add0(m) + '-' + add0(d);
}
//获取年月日时分秒
const getCurrentDate1 = () => {
  const time = new Date();
  const y = time.getFullYear();
  let m = time.getMonth() + 1;
  let d = time.getDate();
  let hour = time.getHours();
  let minute = time.getMinutes();
  let second = time.getSeconds();
  return y + '-' + add0(m) + '-' + add0(d) + ' ' +add0(hour)+':'+add0(minute)+':'+add0(second);
}


//获取年月
const getCurrentYearAndMont = () => {
  const time = new Date();
  const y = time.getFullYear();
  let m = time.getMonth() + 1;
  return y + '-' + add0(m);
}

const dateLimit = (date1, date2, limit) => {
//debugger;
  var a = date1.substr(5, 2);//月
  var b = date1.substr(8, 2);//日
  var c = date1.substr(0, 4);//年
  var a2 = date2.substr(5, 2);//月
  var b2 = date2.substr(8, 2);//日
  var c2 = date2.substr(0, 4);//年
  //alert(date1 + "__" + date2 + "\n\n" + c + "__" + a + "__" + b + "\n\n" + c2 + "__" + a2 + "__" + b2);
  if (c === c2) {
    if (a == a2 && b >= b2) {
      if (Number(b) - Number(b2) <= limit) {
        //alert(Number(b) + "-" + Number(b2) + "=" + (Number(b) - Number(b2)));
        return true;
      }
    } else if (Number(a) - Number(a2) == 1) {
      //alert(Number(a) + "-" + Number(a2) + "=" + (Number(a) - Number(a2)));
      if (30 + Number(b) - Number(b2) <= limit) {
        return true;
      }
    }
  } else if (c - c2 === 1) {
    if ((Number(a) == 1) && (Number(a2) == 12)) {
      if (30 + Number(b) - Number(b2) <= limit) {
        return true;
      }
    }
  } else if (c2 - c === 1) {
    if ((Number(a2) == 1) && (Number(a) == 12)) {
      if (30 + Number(b2) - Number(b) <= limit) {
        return true;
      }
    }
  }
  return false;
}


/***
 * 获取日期
 * @params { date } 日期
 * @params { number } 获取当天日期或当天日期前后n天
 * @author xiongyouliang
 * @version 2020-10-30 09:19
 * @return {void}
 */


const getSomeDate = (date,n) => {
  let time = "";
  if(n&&n>0){
    time = (new Date(date)).getTime() + Math.abs(n)*24 * 60 * 60 * 1000;
    time = new Date(time);
  }else if(n && n<0){
    time = (new Date).getTime() - Math.abs(n)*24 * 60 * 60 * 1000;
    time = new Date(time)
  }else{
    time = new Date();
  }

  const year = time.getFullYear();
  let month = time.getMonth()+1;
  let day = time.getDate();
  if(month<10){
    month = "0"+month;
  }
  if(day<10){
    day = "0"+day;
  }
  return `${year}-${month}-${day}`
}

//比较两个日期早晚
export const judgeDate = (date1,date2) =>{
  const oDate1 = new Date(date1);
  const oDate2 = new Date(date2);
  if(oDate1.getTime() > oDate2.getTime()){
    return true
  } else {
    return false
  }
}
/**
 * 判断是否是日期格式
 * @param str
 * @returns {boolean}
 */
const strDateTime =(str)=>
{
  let r = str.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
  if(r==null)return false;
  let d= new Date(r[1], r[3]-1, r[4]);
  return (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]);
}
/**
 * 格式化数据
 * @param numb
 * @param format
 * @returns {string}
 */
const formatDate =(numb, format)=>{
  const time = new Date((numb - 1) * 24 * 3600000 + 1)
  time.setYear(time.getFullYear() - 70)
  const year = time.getFullYear() + ''
  const month = time.getMonth() + 1 + ''
  const date = time.getDate() - 1 + ''
  if (format && format.length === 1) {
    return year + format + (month < 10 ? '0' + month : month) + format + (date < 10 ? '0' + date : date)
  }
  return year + (month < 10 ? '0' + month : month) + (date < 10 ? '0' + date : date)
}

//两个日期的差值(d1 - d2).
const dateDiff = (d1,d2,params=1000)=>{
  let day = 24 * 60 * 60 *params;
  try{
    let dateArr = d1.split("-");
    let checkDate = new Date();
    checkDate.setFullYear(dateArr[0], dateArr[1]-1, dateArr[2]);
    let checkTime = checkDate.getTime();

    let dateArr2 = d2.split("-");
    let checkDate2 = new Date();
    checkDate2.setFullYear(dateArr2[0], dateArr2[1]-1, dateArr2[2]);
    let checkTime2 = checkDate2.getTime();

    let cha = Math.round((checkTime - checkTime2)/day);
    return cha;
  }catch(e){
    return false;
  }
}
/**
 * 两个日期相差天数
 * @param date1
 * @param date2
 * @returns {number}
 */
const minusDate = (date1,date2)=>{
  return moment(date1).diff(moment(date2),'days');
}
export {timeToEng,minusDate, timeFormat, dateFormat, timestampTranslateToDate, timestampTranslateToDate1, getCurrentDate, getCurrentDate1,getIntervalHour, getCurrentYearAndMont, dateLimit, getSomeDate ,dateGetYMD,strDateTime,formatDate,dateDiff}
